<template>
  <view class="container">
    <!-- 订单信息 -->
    <view class="order-section">
      <view class="amount">
        <text class="label">支付金额</text>
        <text class="price">¥{{orderInfo.amount}}</text>
      </view>
      <view class="order-no">订单号：{{orderInfo.orderNo}}</view>
    </view>
    
    <!-- 支付方式 -->
    <view class="payment-section">
      <text class="section-title">支付方式</text>
      <view class="payment-list">
        <view 
          class="payment-item" 
          v-for="(item, index) in paymentList" 
          :key="index"
          :class="{ active: selectedPayment === item.value }"
          @tap="selectPayment(item.value)"
        >
          <view class="left">
            <text class="iconfont" :class="item.icon"></text>
            <text>{{item.label}}</text>
          </view>
          <text class="iconfont icon-check" v-if="selectedPayment === item.value"></text>
        </view>
      </view>
    </view>
    
    <!-- 支付密码 -->
    <view class="password-section" v-if="selectedPayment === 'password'">
      <text class="section-title">支付密码</text>
      <view class="password-input">
        <view 
          class="input-item" 
          v-for="(item, index) in 6" 
          :key="index"
        >
          <text v-if="password[index]">*</text>
        </view>
      </view>
      <input 
        type="password" 
        v-model="password" 
        maxlength="6"
        @input="handlePasswordInput"
      />
    </view>
    
    <!-- 支付按钮 -->
    <view class="submit-section">
      <button class="submit-btn" @tap="handlePay">立即支付</button>
    </view>
  </view>
</template>

<script>
import { orderApi, paymentApi } from '@/api/index'
import { showToast } from '@/utils/common'

export default {
  data() {
    return {
      orderId: '',
      orderInfo: {
        orderNo: '',
        amount: '0.00'
      },
      paymentList: [
        { label: '余额支付', value: 'balance', icon: 'icon-wallet' },
        { label: '微信支付', value: 'wechat', icon: 'icon-wechat' },
        { label: '支付宝支付', value: 'alipay', icon: 'icon-alipay' },
        { label: '密码支付', value: 'password', icon: 'icon-lock' }
      ],
      selectedPayment: 'balance',
      password: ''
    }
  },
  onLoad(options) {
    this.orderId = options.orderId
    this.getOrderDetail()
  },
  methods: {
    // 获取订单详情
    async getOrderDetail() {
      try {
        const res = await orderApi.getOrderDetail(this.orderId)
        this.orderInfo = res
      } catch (error) {
        showToast('订单信息加载失败')
      }
    },
    
    // 选择支付方式
    selectPayment(value) {
      this.selectedPayment = value
      this.password = ''
    },
    
    // 处理密码输入
    handlePasswordInput(e) {
      const value = e.detail.value
      if (value.length === 6) {
        this.handlePay()
      }
    },
    
    // 处理支付
    async handlePay() {
      if (this.selectedPayment === 'password' && this.password.length !== 6) {
        showToast('请输入6位支付密码')
        return
      }
      
      try {
        const res = await paymentApi.pay({
          orderId: this.orderId,
          paymentMethod: this.selectedPayment,
          password: this.password
        })
        
        if (res.success) {
          showToast('支付成功')
          setTimeout(() => {
            uni.redirectTo({
              url: '/pages/order/list'
            })
          }, 1500)
        } else {
          showToast(res.message || '支付失败')
        }
      } catch (error) {
        showToast('支付失败')
      }
    }
  }
}
</script>

<style lang="scss">
.container {
  min-height: 100vh;
  background: #f5f5f5;
  padding-bottom: 120rpx;
}

.order-section {
  background: #ff0000;
  padding: 40rpx 30rpx;
  
  .amount {
    margin-bottom: 20rpx;
    
    .label {
      font-size: 28rpx;
      color: rgba(255, 255, 255, 0.8);
      margin-right: 20rpx;
    }
    
    .price {
      font-size: 48rpx;
      color: #fff;
      font-weight: bold;
    }
  }
  
  .order-no {
    font-size: 24rpx;
    color: rgba(255, 255, 255, 0.8);
  }
}

.payment-section {
  background: #fff;
  padding: 30rpx;
  margin-bottom: 20rpx;
  
  .section-title {
    font-size: 28rpx;
    color: #333;
    margin-bottom: 20rpx;
    display: block;
  }
  
  .payment-list {
    .payment-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 20rpx 0;
      border-bottom: 1rpx solid #f5f5f5;
      
      &:last-child {
        border-bottom: none;
      }
      
      .left {
        display: flex;
        align-items: center;
        
        .iconfont {
          font-size: 40rpx;
          color: #666;
          margin-right: 20rpx;
        }
        
        text {
          font-size: 28rpx;
          color: #333;
        }
      }
      
      .icon-check {
        font-size: 32rpx;
        color: #ff0000;
      }
      
      &.active {
        .left {
          .iconfont {
            color: #ff0000;
          }
          
          text {
            color: #ff0000;
          }
        }
      }
    }
  }
}

.password-section {
  background: #fff;
  padding: 30rpx;
  position: relative;
  
  .section-title {
    font-size: 28rpx;
    color: #333;
    margin-bottom: 20rpx;
    display: block;
  }
  
  .password-input {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20rpx;
    
    .input-item {
      width: 80rpx;
      height: 80rpx;
      border: 2rpx solid #ddd;
      border-radius: 8rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      
      text {
        font-size: 40rpx;
        color: #333;
      }
    }
  }
  
  input {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
  }
}

.submit-section {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 20rpx 30rpx;
  background: #fff;
  
  .submit-btn {
    width: 100%;
    height: 80rpx;
    line-height: 80rpx;
    text-align: center;
    background: #ff0000;
    color: #fff;
    border-radius: 40rpx;
    font-size: 28rpx;
  }
}
</style> 